package com.orhon.smartcampus.modules.teacher.mutation;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.orhon.smartcampus.auth.JwtUtils;
import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.base.dao.SchoolsDao;
import com.orhon.smartcampus.modules.teacher.dao.PracticingQualificationDao;
import com.orhon.smartcampus.modules.teacher.dao.TInformationDao;
import com.orhon.smartcampus.modules.teacher.entity.PracticingQualification;
import com.orhon.smartcampus.modules.teacher.gqlInputModel.GQLPracticingQualificationInput;
import com.orhon.smartcampus.modules.teacher.inputMapper.PracticingQualificationInputMapper;
import com.orhon.smartcampus.modules.user.dao.TokenService;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

@Component
public class AddPracticingQualficationMutation implements DataFetcher {

    @Autowired
    PracticingQualificationDao practicingQualificationDao;

    @Autowired
    TInformationDao tInformationDao;

    @Autowired
    SchoolsDao schoolsDao;

    @Autowired
    TokenService tokenService;

    /**
     * 添加专职(执)业资格
     * editor : ths
     */
    @Override
    public Object get(DataFetchingEnvironment environment) throws Exception {

        //String token = (String) SecurityUtils.getSubject().getPrincipal();
        String token = tokenService.getSystemToken();
        Long schoolId = JwtUtils.getSchoolId(token);
        GQLPracticingQualificationInput input = PracticingQualificationInputMapper.MAPPER.mapInput(environment.getArgument("inputData"));

        PracticingQualification practicingQualification = null;

        if (input.getId() != null) {
            practicingQualification = practicingQualificationDao.get(PracticingQualification.class, input.getId());
            practicingQualification.setUpdatedAt(new Date());
        } else {
            practicingQualification = new PracticingQualification();
            practicingQualification.setCreatedAt(new Date());
        }
        if(schoolId != null){

            practicingQualification.setSchool_id(schoolId);
        }
        if(input.getTeacher_id() != null){

            practicingQualification.setTeacher_id(input.getTeacher_id());
        }

        if(input.getInputstyle() != null){

            practicingQualification.setInputstyle(input.getInputstyle());
        }
        if(input.getTakedate() != null){

            practicingQualification.setTakedate(input.getTakedate());
        }
        if(input.getCertificateno() != null){

            practicingQualification.setCertificateno(input.getCertificateno());
        }
        if(input.getImage() != null){

            practicingQualification.setImage(input.getImage());
        }
        if(input.getShenhe() != null){

            practicingQualification.setShenhe(input.getShenhe());
        }
        if(input.getAwarddept() != null){

            practicingQualification.setAwarddept(JSON.toJSONString(input.getAwarddept()));
        }
        if(input.getName() != null){

            practicingQualification.setName(JSON.toJSONString(input.getName()));
        }
        if(input.getGrade() != null){

            practicingQualification.setGrade(JSON.toJSONString(input.getGrade()));
        }
        try{
            practicingQualificationDao.saveOrUpdate(practicingQualification);
        }
        catch (Exception e){
            return Result.error(500,""+e);
        }
        return Result.ok();
    }

//	public static void main(String[] args) {
//		passport.setSchool_id(schoolId);
//		        passport.setTeacher_id( input.getTeacher_id());
//	}
}

